local thbattle__huiyin_shouhu = fk.CreateSkill {

  name = "thbattle__huiyin_shouhu",

  tags = { Skill.Limited, },

}



thbattle__huiyin_shouhu:addEffect(fk.EventPhaseStart, {
  anim_type = "big",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(thbattle__huiyin_shouhu.name) and player.phase == Player.Play and
        table.find(player.room:getOtherPlayers(player), function(p)
          return p:isWounded() and p:getMark("@thbattle__huiyin_jueyi") == 0
        end) and player:usedSkillTimes(thbattle__huiyin_shouhu.name, Player.HistoryGame) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if room:askForSkillInvoke(player, thbattle__huiyin_shouhu.name, nil, "#thbattle__huiyin_shouhu") then
      local target_p = room:askForChoosePlayers(player,
        table.map(table.filter(player.room:getOtherPlayers(player), function(p)
          return p:isWounded()
        end), Util.IdMapper), 1, 1, "#thbattle__huiyin_shouhu-choose_p", thbattle__huiyin_shouhu.name, true)
      if #target_p > 0 then
        event:setCostData(self, target_p[1])
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local target_p = room:getPlayerById(event:getCostData(self))
    room:changeMaxHp(player, -1)
    room:recover({
      who = target_p,
      num = 1,
      recoverBy = player,
      skillName = thbattle__huiyin_shouhu.name
    })
    if table.every(room:getOtherPlayers(target_p), function(p)
          return p.hp >= target_p.hp
        end) then
      room:handleAddLoseSkills(player, "thbattle__huiyin_jueyi")
      room:handleAddLoseSkills(target_p, "thbattle__huiyin_jueyi")
      room:setPlayerMark(player, "thbattle__huiyin_jueyi", target_p.id)
      room:setPlayerMark(target_p, "thbattle__huiyin_jueyi", player.id)
      room:setPlayerMark(player, "@thbattle__huiyin_jueyi", Fk:translate(target_p.general))
      room:setPlayerMark(target_p, "@thbattle__huiyin_jueyi", Fk:translate(player.general))
      room:setPlayerMark(player, "@!thbattle__huiyin_jueyi", 1)
      room:setPlayerMark(target_p, "@!thbattle__huiyin_jueyi", 1)
    end
  end,
})

return thbattle__huiyin_shouhu